package com.pipayshop.controller;

import com.pipayshop.entity.ShopCommodityLiveInfo;
import com.pipayshop.entity.dto.RoomDto;
import com.pipayshop.entity.dto.ShopCommodityLiveInfoListDTO1;
import com.pipayshop.entity.dto.ShopHotelRecordDTO;
import com.pipayshop.entity.vo.*;
import com.pipayshop.exception.BusinessException;
import com.pipayshop.service.HotelShopCommodityLiveInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


@Api(value = "房型相关接口", tags = "房型相关接口")
@RestController
@RequestMapping("/pipayshopapi/shop-commodity-live-info")
@Slf4j
public class HotelRoomController {

    @Resource
    private HotelShopCommodityLiveInfoService shopCommodityLiveInfoService;

    @GetMapping("selectShopLiveByRoomId/{roomId}")
    @ApiOperation("根据房型id查找房型的详细信息")
    public ResponseVO<ShopCommodityLiveInfoVO5> selectShopLiveByRoomId(@PathVariable String roomId) {
        try {
            ShopCommodityLiveInfoVO5 result = shopCommodityLiveInfoService.selectShopLiveByRoomId(roomId);
            return ResponseVO.getSuccessResponseVo(result);
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        }  catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("query_room_type_details_by_id_failed_contact_staff");
        }
    }

    @PostMapping("deleteShopLiveInfo/{roomId}")
    @ApiOperation("根据房型id删除房型的详细信息")
    public ResponseVO<String> deleteRoom(@PathVariable String roomId) {
        try {
            if (shopCommodityLiveInfoService.deleteShopLiveInfo(roomId)) {
                return ResponseVO.getSuccessResponseVo("delete_room_type_details_by_id_success");
            } else {
                throw new Exception();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("delete_room_type_details_by_id_failed_contact_staff");
        }
    }

    @PostMapping("applyShopCommodityLive")
    @ApiOperation("提交入住酒店")
    public ResponseVO<String> applyShopCommodityLive(@RequestBody ShopHotelRecordDTO shopHotelRecordDTO) {
        try {
            if (shopCommodityLiveInfoService.applyShopCommodityLive(shopHotelRecordDTO)) {
                return ResponseVO.getSuccessResponseVo("submit_checkin_success");
            } else {
                throw new Exception();
            }
        } catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("submit_checkin_failed_contact_staff");
        }
    }

    @PostMapping("updateShopLive")
    @ApiOperation("根据房型id更改房型信息")
    public ResponseVO<String> updateShopLive(@RequestBody ShopCommodityLiveInfoUpVO shopCommodityLiveInfoUpVO) {
        try {
            if (shopCommodityLiveInfoService.updateShopLive(shopCommodityLiveInfoUpVO)) {
                return ResponseVO.getSuccessResponseVo("update_room_type_info_by_id_success");
            } else {
                throw new Exception();
            }
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("update_room_type_info_by_id_failed_contact_staff");
        }
    }

    @PostMapping("updateShopLiveInfo")
    @ApiOperation("根据房型id更改房型的详细信息")
    public ResponseVO<String> updateShopLiveInfo(@RequestBody ShopCommodityLiveInfo shopCommodityLiveInfo) {
        try {
            if (shopCommodityLiveInfoService.updateShopLiveInfo(shopCommodityLiveInfo)) {
                return ResponseVO.getSuccessResponseVo("update_room_type_details_by_id_success");
            } else {
                throw new Exception();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("update_room_type_details_by_id_failed_contact_staff");
        }
    }

    @PostMapping("/selectAllByPrice")
    @ApiOperation("价格升降序查房")
    public ResponseVO selectAllByPrice(@RequestBody ShopCommodityLiveInfoListDTO1 shopInfo) {
        try {
            return ResponseVO.getSuccessResponseVo(shopCommodityLiveInfoService.selectAllByPrice(shopInfo));
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("sort_rooms_by_price_failed_contact_staff");
        }
    }

    @PostMapping("/takeOffRoom")
    @ApiOperation("上（0）下架（1）/删除（3）/酒店房间")
    public ResponseVO<String> takeOffRoom(@RequestBody RoomDto room) {
        try {
            int result = shopCommodityLiveInfoService.updateRoomStatus(room);
            return result == 1 ? ResponseVO.getSuccessResponseVo("room_status_update_success") : ResponseVO.getFalseResponseVo("room_status_update_failed_contact_staff");
        }catch (BusinessException e) {
            throw new BusinessException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("room_status_update_failed_contact_staff");
        }
    }


}
